package com.gitee.chessboard.tools;

import com.gitee.chessboard.db.DictionaryService;
import com.gitee.chessboard.manager.DictionaryManager;
import com.gitee.chessboard.model.dictionary.Dictionary;
import com.gitee.chessboard.model.word.Word;
import com.intellij.openapi.components.ServiceManager;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.wm.ToolWindow;

import javax.swing.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author 敲代码的旺财
 * @date 2021/12/20 9:51
 */
public class DictionaryManagerToolsForm {
    private JPanel mainPanel;
    private JList<Dictionary> dictionaryList;
    private JList<String> dictionaryAttrList;
    private JLabel dictionaryLabel;
    private JLabel wordLabel;
    private JList<Word> wordList;
    private JList<String> wordAttrList;
    private JLabel dictionaryAttrLabel;
    private JLabel wordAttrLabel;

    public DictionaryManagerToolsForm(Project project, ToolWindow toolWindow) {
        init(project);

        dictionaryList.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                JList<Dictionary> jList = (JList<Dictionary>) e.getSource();
                if (e.getClickCount() >= 1) {
                    Dictionary selectedValue = jList.getSelectedValue();
                    setDictionaryAttrListModel(selectedValue);

                    List<Word> words = selectedValue.get();
                    ListModel<Word> wordsListModel = new DefaultComboBoxModel<>(words.toArray(Word[]::new));
                    wordList.setModel(wordsListModel);
                    if (words.size() > 0) {
                        Word word = words.get(0);
                        setWordAttrListModel(word);
                    }
                }
            }
        });

        wordList.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                JList<Word> jList = (JList<Word>) e.getSource();
                if (e.getClickCount() >= 1) {
                    Word selectedValue = jList.getSelectedValue();
                    setWordAttrListModel(selectedValue);
                }
            }
        });

    }

    private void init(Project project) {
        DictionaryService dictionaryService = ServiceManager.getService(project, DictionaryService.class);
        DictionaryManager dictionaryManager = dictionaryService.getDictionaryManager();
        List<Dictionary> dictionaries = dictionaryManager.get();

        ListModel<Dictionary> dictionaryListModel = new DefaultComboBoxModel<>(dictionaries.toArray(Dictionary[]::new));
        dictionaryList.setModel(dictionaryListModel);

        if (dictionaries.size() > 0) {
            Dictionary dictionary = dictionaries.get(0);
            setDictionaryAttrListModel(dictionary);

            List<Word> words = dictionary.get();
            ListModel<Word> wordsListModel = new DefaultComboBoxModel<>(words.toArray(Word[]::new));
            wordList.setModel(wordsListModel);

            if (words.size() > 0) {
                Word word = words.get(0);
                setWordAttrListModel(word);
            }
        }
    }

    private void setWordAttrListModel(Word word) {
        List<String> wordAttr = new ArrayList<>();
        String wordName = "名称：" + word.getWordName();
        String wordCode = "编码：" + word.getWordCode();
        wordAttr.add(wordName);
        wordAttr.add(wordCode);
        for (Map.Entry<String, String> entry : word.getWordAttr().entrySet()) {
            wordAttr.add(entry.getKey() + "：" + entry.getValue());
        }
        ListModel<String> wordAttrListModel = new DefaultComboBoxModel<>(wordAttr.toArray(String[]::new));
        wordAttrList.setModel(wordAttrListModel);
    }

    private void setDictionaryAttrListModel(Dictionary dictionary) {
        String dictionaryName = "名称：" + dictionary.getDictionaryName();
        String dictionaryCode = "编码：" + dictionary.getDictionaryCode();
        String dictionaryType = "类型：" + dictionary.getDictionaryType();

        ListModel<String> dictionaryAttrListModel = new DefaultComboBoxModel<String>(
                new String[]{dictionaryName, dictionaryCode, dictionaryType});
        dictionaryAttrList.setModel(dictionaryAttrListModel);
    }


    public JPanel getMainPanel() {
        return mainPanel;
    }
}
